package com.apestech.api.exp;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.LocalVariable;
import com.apestech.framework.xml.ReadXmlNodeInter;
import com.apestech.framework.xml.plugin.XmlPaste;
import org.dom4j.Element;

import java.io.File;
import java.util.ArrayList;

public class ExpUtil {
    private static JSONObject mapping;
    private static JSONObject sysMap;
    private static JSONObject tabmap=new JSONObject();
    private static JSONObject xlsmap=new JSONObject();

    public static JSONObject getMapping() throws Exception {
        if(mapping==null){
            XmlPaste xml=new XmlPaste(LocalVariable.getRootDir()+"config"+ File.separator+"expconfig.xml");
            xml.setNode("tableMap");
            xml.expain();
            mapping=xml.getResult();
        }
        return mapping;
    }

    public static JSONObject getSysMap() throws Exception {
        if(sysMap==null) sysMap=(JSONObject) mapping.get("SysMap");
        return sysMap;
    }

    public static JSONObject getTabSysMap(String tabname) throws Exception {
        JSONObject tm=(JSONObject) mapping.get(tabname);
        if(tm!=null){
            return (JSONObject) tm.get("SysMap");
        }
        return null;
    }

    public static ExpTable getExp(String id) throws Exception {
        if(tabmap.get(id)==null){
            XmlPaste xml=new XmlPaste();
            xml.getXmlQuery().setFileDir(LocalVariable.getRootDir()+"temp"+ File.separator+"script"+ File.separator+id+".xml");
            ArrayList<String> nlist=new ArrayList<String>();
            nlist.add("expTable");
            xml.getXmlQuery().getXmlFileNode(nlist, new ReadXmlNodeInter(){
                @Override
                public void ReadNode(Element root, Element node, String nodename) throws Exception {
                    String sid=node.attributeValue("id");
                    xml.expain(root, node);
                    setExpTable(sid, xml.getSingleResult());
                }
            });
        }
        return (ExpTable) tabmap.get(id);
    }

    private static void setExpTable(String id, JSONObject m){
        tabmap.put(id, new ExpTable(m));
    }

    public static XLSTable getXls(String id) throws Exception {
        if(xlsmap.get(id)==null){
            XmlPaste xml=new XmlPaste();
            xml.getXmlQuery().setFileDir(LocalVariable.getRootDir()+"temp"+ File.separator+"script"+ File.separator+"XLS_READ.xml");
            ArrayList<String> nlist=new ArrayList<String>();
            nlist.add("xlsRead");
            xml.getXmlQuery().getXmlFileNode(nlist, new ReadXmlNodeInter(){
                @Override
                public void ReadNode(Element root, Element node, String nodename) throws Exception {
                    String sid=node.attributeValue("id");
                    xml.expain(root, node);
                    xlsmap.put(sid, new XLSTable(xml.getSingleResult()));
                }
            });
        }
        return (XLSTable) xlsmap.get(id);
    }

}
